﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DelonixRegiaHMSys.Core.Services;
using DelonixRegiaHMSys.Core.Models;
using DelonixRegiaHMSys.Infrastructure.Data;

namespace DelonixRegiaHMSys.UI.WebForms
{
    public partial class RoomSelectionFromReservation : System.Web.UI.Page
    {
        string userID;
        string guestID;
        string reservationID;
        string numOfA;
        string numOfC;
        string rT;
        string bT;
        string sON;
        string aR;
        string aD;
        string rS;
        protected void Page_Load(object sender, EventArgs e)
        {
            userID = Request.QueryString["UserID"];
            guestID = Request.QueryString["GuestID"];
            reservationID = Request.QueryString["ReservationID"];
            numOfA = Request.QueryString["NumOfAdults"];
            numOfC = Request.QueryString["NumOfChildren"];
            rT = Request.QueryString["RoomType"];
            bT = Request.QueryString["BedType"];
            sON = Request.QueryString["SmokingOrNot"];
            aR = Request.QueryString["AddRemarks"];
            aD = Request.QueryString["ArrivalDate"];
            rS = Request.QueryString["ReservationStatus"];
            if (!IsPostBack)
            {
                BindGridView();
            }
        }

        private void BindGridView()
        {
            var roservice = new roServices(new EfRoomQuery());
            DataListRoomAvail.DataSource = roservice.GetRoomByGuestRequirements(rT, bT, sON).ToList();
            DataListRoomAvail.DataBind();
        }

        protected void lrbtn_Click(object sender, EventArgs e)
        {
            tbxroom.Text = (((LinkButton)sender).CommandArgument);
        }

        protected void CheckIn_Click(object sender, EventArgs e)
        {
            var ciservice = new ciServices(new EfCheckInQuery());
            var roservice = new roServices(new EfRoomQuery());
            CheckIn ci = new CheckIn();
            ci.CheckInID = lblcheckin.Text;
            ci.GuestID = guestID;
            ci.ReservationID = reservationID;
            ci.UserID = userID;
            ci.NumOfAdults = Convert.ToInt32(numOfA);
            ci.NumOfChildren = Convert.ToInt32(numOfC);
            ci.RoomType = rT;
            ci.BedType = bT;
            ci.SmokingOrNot = sON;
            ci.CheckInDateTime = DateTime.Now;
            ci.DesiredCheckOutDateTime = Convert.ToDateTime(tbxdesiredCheckOutDate.Text);
            ci.AddRemarks = aR;
            ci.RoomNum = Convert.ToInt32(tbxroom.Text);
            ci.PaymentMode = paymentMode.Text;
            ci.LateCheckOutRequest = RadioButtonList1.SelectedValue;
            ci.Status = "SCI";
            ciservice.AddCheckIn(ci);

            bool SuccessOrFail = ciservice.CheckInRecordAdded(ci.CheckInID);
            if (SuccessOrFail == true)
            {
                roservice.UpdateRoom(ci.RoomNum, "occupied", ci.GuestID);
                string queryString = "CheckInID=" + ci.CheckInID;
                Server.Transfer("~/WebForms/RRandCIModule/AddRecordSuccess.aspx?" + queryString);
            }
            else
            {

            }
        }
    }
}